package com.portme.client.helpers;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.portme.client.PortmeServiceClient;
import com.portme.client.StaticFactory;

public class ThemeDialogBox extends DialogBox{
	
	protected AsyncCallback<String> callback = new AsyncCallback<String>() {
		
		@Override
		public void onSuccess(String result) {
			StaticFactory.getInstance().setTheme(result);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert("The changes were not saved.");
		}
	};

	public ThemeDialogBox() {
		super(true);		
		PortMeThemes themes = new PortMeThemes();
		HashMap<String, String> themesMap = themes.getThemes();
		VerticalPanel vp = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();
		HorizontalPanel hp1 = new HorizontalPanel();
		int index = 0;
		Iterator<Entry<String, String>> it = themesMap.entrySet().iterator();
		while(it.hasNext())
		{
			final Entry<String, String> entry = it.next();
			RadioButton radioButton = new RadioButton("theme");
			radioButton.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					PortmeServiceClient.getInstance().setTheme(entry.getKey(), callback );
				}
			});
			
			Image img = new Image(entry.getValue());
			img.setSize("125px", "125px");
			if(index < 5)
			{
				hp.add(radioButton);
				hp.add(img);
			}
			else 
			{
				hp1.add(radioButton);
				hp1.add(img);
			}
			index++;
		}
		vp.add(hp);
		vp.add(hp1);
		this.add(vp);
	}	
}
